import { createContext, useState, useContext, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as SplashScreen from 'expo-splash-screen';

const Context = createContext();

export const useInstall = ()=>{
    return useContext(Context);
}

const Provider = (props)=>{
    const [isReady,setIsReady] = useState(false);
    const [isInstall,setIsInstall] = useState(false);
    useEffect(()=>{
        SplashScreen.preventAutoHideAsync();
        AsyncStorage.getItem('install')
            .then(res=>{
                if(res === 'true') {
                    setIsInstall(true);
                } else {
                    setIsInstall(false)
                }
                setIsReady(true);
                SplashScreen.hideAsync();
            })
    }, [])
    if(!isReady) {
        return null;
    }

    return <Context.Provider value={{isInstall,setIsInstall}}>
        {props.children}
    </Context.Provider>
}

export default Provider;